﻿using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MyCalManager.Data;
using MyCalManager.Helper;
using MyCalManager.Properties;

namespace MyCalManager.ViewModel
{
    
    public class UserViewModel : ViewModelBase
    {
        private CoreData data = CoreData.Instance;

        private bool CanExecute = false;
        private bool CanDelUser = false;
        private Settings settings;

        /// <summary>
        /// Initializes a new instance of the UserViewModel class.
        /// </summary>
        public UserViewModel()
        {
            Initialize();
        }

        private void Initialize()
        {
            this.settings = Settings.Default;
            this.Users = this.data.UserList;
            
            this.AddUser = new RelayCommand(
                () => OnAddUser(),
                () => CanExecute);

            this.DelUser = new RelayCommand(
                () => OnDelUser(),
                () => CanDelUser);

            this.Ok = new RelayCommand(
                () => OnOk());

            this.Cancel = new RelayCommand(
                () => OnCancel());
        }

        

        

        

        #region Commands and CommandMethods

        public RelayCommand AddUser
        {
            get;
            private set;
        }

        public RelayCommand DelUser
        {
            get;
            private set;
        }

        public RelayCommand Ok
        {
            get;
            private set;
        }

        public RelayCommand Cancel
        {
            get;
            private set;
        }


        private void OnAddUser()
        {
            User newUser = new User();
            newUser.Name = this.Name;
            newUser.Vorname = this.VorName;
            if (this.data.AddUser(newUser))
            {
                this.Name = string.Empty;
                this.VorName = string.Empty;
                this.Users.Add(newUser);
                CustomDialogMessage m = new CustomDialogMessage();
                m.isDialog = false;
                m.Content = "User angelegt !";
                m.customMessageBoxImage = CustomMessageBoxImage.Information;
                Messenger.Default.Send(m);
            }
        }

        private void OnDelUser()
        {
            if (this.data.DeleteUser(this.selectedUser))
            {
                this.Users.Remove(this.selectedUser);
                CustomDialogMessage m = new CustomDialogMessage();
                m.isDialog = false;
                m.Content = "User gelöscht !";
                m.customMessageBoxImage = CustomMessageBoxImage.Information;
                Messenger.Default.Send(m);
            }
        }

        private void OnOk()
        {
            Navigator n = new Navigator();

            if (this.selectedUser == null)
            {
                n.Result = NavigateResult.None;
                n.To = NavigateTo.Main;
                Messenger.Default.Send(n);
                return;
            }

            if ((bool)defaultUser)
            {
                this.settings.UserID = this.selectedUser.ID;
                this.settings.Save();
            }

            this.data.UserID = this.selectedUser.ID;
            
            n.To = NavigateTo.Main;
            n.Result = NavigateResult.UserChanged;
            Messenger.Default.Send(n);
        }

        private void OnCancel()
        {
            Navigator n = new Navigator();
            n.To = NavigateTo.Main;
            n.Result = NavigateResult.Cancel;
            Messenger.Default.Send(n);
        }

        #endregion



        #region Public Properties

        public const string visibilityPropertyName = "visibility";
        private bool _visibility = true;
        public bool visibility
        {
            get
            {
                return _visibility;
            }

            set
            {
                if (_visibility == value)
                {
                    return;
                }


                _visibility = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(visibilityPropertyName);

            }
        }

       
        public const string UsersPropertyName = "Users";
        private ObservableCollection<User> _myUsers;
        public ObservableCollection<User> Users
        {
            get
            {
                return _myUsers;
            }

            set
            {
                if (_myUsers == value)
                {
                    return;
                }
                
                _myUsers = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(UsersPropertyName);

            }
        }

        
        public const string selectedUserPropertyName = "selectedUser";
        private User _myselectedUser;
        public User selectedUser
        {
            get
            {
                return _myselectedUser;
            }

            set
            {
                if (_myselectedUser == value)
                {
                    return;
                }
               
                _myselectedUser = value;

                if (_myselectedUser == null)
                    this.CanDelUser = false;
                else
                {
                    this.CanDelUser = true;

                    if (this._myselectedUser.ID == this.settings.UserID)
                        this.defaultUser = true;
                    else
                        this.defaultUser = false;
                }
                // Update bindings, no broadcast
                RaisePropertyChanged(selectedUserPropertyName);

                
            }
        }

        public const string NameValidPropertyName = "NameValid";
        private bool _myNameValid;
        public bool NameValid
        {
            get
            {
                return _myNameValid;
            }

            set
            {
                if (_myNameValid == value)
                {
                    return;
                }

               
                _myNameValid = value;

                

                // Update bindings, no broadcast
                RaisePropertyChanged(NameValidPropertyName);
                this.CanExecute = VorNameValid && NameValid;
            }
        }

        
        public const string NamePropertyName = "Name";
        private string _myName = string.Empty;
        public string Name
        {
            get
            {
                return _myName;
            }

            set
            {
                if (_myName == value)
                {
                    return;
                }

                
                _myName = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(NamePropertyName);
                
            }
        }

        public const string VorNameValidPropertyName = "VorNameValid";
        private bool _myVorNameValid;
        public bool VorNameValid
        {
            get
            {
                return _myVorNameValid;
            }

            set
            {
                if (_myVorNameValid == value)
                {
                    return;
                }


                _myVorNameValid = value;



                // Update bindings, no broadcast
                RaisePropertyChanged(VorNameValidPropertyName);
                this.CanExecute = VorNameValid && NameValid;

            }
        }

        public const string VorNamePropertyName = "VorName";
        private string _myVorName = string.Empty;
        public string VorName
        {
            get
            {
                return _myVorName;
            }

            set
            {
                if (_myVorName == value)
                {
                    return;
                }


                _myVorName = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(VorNamePropertyName);

            }
        }

        
        public const string defaultUserPropertyName = "defaultUser";
        private bool? _defaultUser = false;
        public bool? defaultUser
        {
            get
            {
                return _defaultUser;
            }

            set
            {
                if (_defaultUser == value)
                {
                    return;
                }
                
                _defaultUser = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(defaultUserPropertyName);

                
            }
        }

        #endregion
    }
}